import pymysql
import csv


def getList(dir):
    with open(dir, encoding='utf-8') as f:
        datas = []
        reader = csv.reader(f)
        for row in reader:
            if len(row[0]) > 0 and len(row[1]) > 0:
                datas.append((row[1], row[0]))
    return datas


if __name__ == '__main__':
    db = pymysql.connect("localhost", "root", "546224", "situation_puzzle")
    cursor = db.cursor()
    datas=getList('dst.csv')
    for question,answer in datas:
        cursor.execute("""insert into puzzle(question,answer) values ('%s','%s');"""%(pymysql.escape_string(question),pymysql.escape_string(answer)))
    db.commit()
    db.close()
    print("Done")
